﻿using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace LS.Screening.UI.Common
{
    /// <summary>
    /// 属性描述信息
    /// </summary>
    public class PropertyDescribe
    {
        /// <summary>
        /// 属性信息
        /// </summary>
        public PropertyInfo PropertyInfo { get; set; }

        /// <summary>
        /// 属性说明文字
        /// </summary>
        public string Display { get; set; }

        /// <summary>
        /// 属性全部标签信息
        /// </summary>
        public List<Attribute> Attributes { get; set; }

        /// <summary>
        /// 属性验证相关标签信息
        /// </summary>
        public IEnumerable<ValidationAttribute> ValidationAttributes => Attributes.Where((Attribute p) => p.GetType().IsAssignableTo(typeof(ValidationAttribute))).Cast<ValidationAttribute>();

        /// <summary>
        /// 获取指定类型的标签定义
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetAttribute<T>() where T : Attribute
        {
            return (T)Attributes.FirstOrDefault((Attribute p) => p.GetType().IsAssignableTo(typeof(T)));
        }

        /// <summary>
        /// 重写ToString
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return PropertyInfo.Name + ":" + Display;
        }
    }
}
